package com.afu.thread;

import java.util.concurrent.*;

/**
 * ClassName: Test
 * Package: com.afu.thread
 * Description:
 *
 * @Author zxf
 * @Create 2024/2/26 16:16
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {
//        继承Thread类
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        ThreadDemo1 threadDemo2 = new ThreadDemo1();
        threadDemo1.start();
        threadDemo2.start();

//=================================================================================================


//        实现Runnable接口
        ThreadRunnableDemo2 threadRunnableDemo21 = new ThreadRunnableDemo2();
        ThreadRunnableDemo2 threadRunnableDemo22 = new ThreadRunnableDemo2();
        Thread thread1 = new Thread(threadRunnableDemo21);
        Thread thread2 = new Thread(threadRunnableDemo22);

        thread1.start();
        thread2.start();

//=================================================================================================


//         匿名实现类
        new Thread(()->{
            System.out.println(Thread.currentThread().getName() +"在执行。。。 2.1匿名实现类");
        }).start();


//=================================================================================================

//        实现Callable接口
        ThreadCallableDemo3 threadCallableDemo31 = new ThreadCallableDemo3();
        ThreadCallableDemo3 threadCallableDemo32 = new ThreadCallableDemo3();

        FutureTask futureTask31 = new FutureTask(threadCallableDemo31);
        FutureTask futureTask32 = new FutureTask(threadCallableDemo32);

        Thread thread31 = new Thread(futureTask31);
        Thread thread32 = new Thread(futureTask32);

        thread31.start();
        thread32.start();

        try {
            Object o1 = futureTask31.get();
            Object o2 = futureTask32.get();
            System.out.println("返回值：" +o1);
            System.out.println("返回值：" +o2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

//=================================================================================================

//        使用线程池
        try {
//            创建一个可根据需要创建新线程的线程池
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,100,60,TimeUnit.SECONDS,new SynchronousQueue<>());
            ExecutorService executorService = Executors.newCachedThreadPool();

//            创建一个可重用固定线程数的线程池
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);

//            创建一个线程池，它可安排在给定延迟后运行命令或者定期地执行。
            ExecutorService scheduledThreadPool= Executors.newScheduledThreadPool(3);

//            返回一个线程池（这个线程池只有一个线程）
            ExecutorService singleThreadScheduledExecutor= Executors.newSingleThreadScheduledExecutor();

            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "在执行。。。  可根据需要创建新线程的线程池");
                }
            });

            executorService.shutdown();
            fixedThreadPool.shutdown();
            scheduledThreadPool.shutdown();
            singleThreadScheduledExecutor.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
